package com.tuniu.tabFragment;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.VolleyError;
import com.tuniu.R;
import com.tuniu.data.mine.Data;
import com.tuniu.data.mine.HomeMenu;
import com.tuniu.data.mine.Menu;
import com.tuniu.data.mine.Mine;
import com.tuniu.utils.Constants;
import com.tuniu.utils.MyAPIUtils;
import com.tuniu.utils.MyAdapter;
import com.tuniu.utils.ObservableScrollView;
import com.xinbo.utils.GsonUtils;
import com.xinbo.utils.HTTPUtils;
import com.xinbo.utils.ResponseListener;
import com.xinbo.utils.UILUtils;
import com.xinbo.widget.GridView4ScrollView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class MineFragment extends Fragment  {

	private View view;
	private LayoutInflater inflater;

	private ArrayList<Menu> menusList = new ArrayList<Menu>();
	private ViewFlipper mFlipper;
	private MyAdapter myAdapter;
	private GridView4ScrollView mGridView;
	private ObservableScrollView mScrollView;
	private RelativeLayout mRelativeLayout;
	private TextView mine_tv_login;
	private ImageView mine_iv_login;
	private RelativeLayout mRelativeLayout2;

	public MineFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.inflater = inflater;
		if (view == null) {
			view = inflater.inflate(R.layout.fragment_mine, container, false);
			initUI();
			initData();
		}

		return view;
	}

	private void initData() {
		
		MyAPIUtils.getMine(getContext(), new ResponseListener() {
			
			@Override
			public void onResponse(String arg0) {
				if (TextUtils.isEmpty(arg0)) {
					Toast.makeText(getContext(), "服务端问题", Toast.LENGTH_SHORT).show();
					return;
				}
				Mine mine = GsonUtils.parseJSON(arg0, Mine.class);
				Data data = mine.getData();
				List<Menu> menus = data.getMenus();
				menusList.clear();
				menusList.addAll(menus);
				viewFlipper();				
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0) {
				
			}
		});
	}

	private void viewFlipper() {
		for (int j = 0; j < menusList.size(); j++) {
			if (j == 2) {
				Menu menu = menusList.get(j);
				List<HomeMenu> homeMenus = menu.getHomeMenus();
				for (int i = 0; i < homeMenus.size(); i++) {
					View layout = inflater.inflate(R.layout.item_viewflipper, null);
					ImageView imagUrl_mine = (ImageView) layout.findViewById(R.id.imageUrl_registration);
					TextView title_mine = (TextView) layout.findViewById(R.id.title_registration);
					String title = homeMenus.get(i).getTitle();
					String imgUrl = homeMenus.get(i).getIcon();
					UILUtils.displayImage(imgUrl, imagUrl_mine);
					title_mine.setText(title);
					mFlipper.addView(layout);
				}
			}
		}

		mFlipper.setInAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.push_up_in));
		mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.push_up_out));
		// 开始垂直滚动动画
		mFlipper.startFlipping();
	}

	private void initUI() {
		mine_iv_login = (ImageView) view.findViewById(R.id.mine_iv_login);
		mine_tv_login = (TextView) view.findViewById(R.id.mine_tv_login);

		//底层布局为背景透明 ，图标文字白色
		mRelativeLayout = (RelativeLayout) view.findViewById(R.id.relative_xuanting);
		//第二层布局为背景白色，图标绿色，文字黑色，默认隐藏
		mRelativeLayout2 = (RelativeLayout) view.findViewById(R.id.relative_xuanting2);
		TextView kaiTong = (TextView) view.findViewById(R.id.mine_kaitong);
		ImageView iv_dingdan = (ImageView) view.findViewById(R.id.mine_iv_dingdan);
		mFlipper = (ViewFlipper) view.findViewById(R.id.mine_viewFlipper);

		mScrollView = (ObservableScrollView) view.findViewById(R.id.scrollView_mine);
		mScrollView.setOnScollChangedListener(new ObservableScrollView.OnScollChangedListener() {
			private float relative_h;

            @Override
            public void onScrollChanged(int height){
			relative_h = mRelativeLayout.getHeight();
			if(relative_h<height){
				//滑动的高度大于悬浮头部高度时，第二层布局显示，不透明   第一层布局由不透明变为透明
				mRelativeLayout2.setAlpha(1);
				mRelativeLayout.setAlpha(height/relative_h);
			
			}else{
				//滑动的高度小于悬浮头部的高度时，第二层布局不透明到完全透明   第一层布局不透明，显示出来
				mRelativeLayout.setAlpha(1);
				mRelativeLayout2.setAlpha(height/relative_h);
			}
            	
            	
            }
        });
		
	}
	


}
